package com.deyuanyun.pic.pipe.service.impl;

import com.deyuanyun.pic.common.dto.BasicDataByFind;
import com.deyuanyun.pic.common.dto.DTOResult;
import com.deyuanyun.pic.common.exception.ExceptionCode;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.pipe.mapper.dao.PipeCrossDao;
import com.deyuanyun.pic.pipe.service.PipeCrossService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 *
 *
 * @作者:严祥
 * @创建时间: 2015年8月19日
 * @类名称: PipeCrossServiceImpl
 * @类说明: 穿跨越基本信息接口实现类
 *
 */
@Service
public class PipeCrossServiceImpl implements PipeCrossService {

	@Resource
	private PipeCrossDao pipeCrossDao;

	@Override
	public List<List<String[]>> getCrossExcel(BasicDataByFind bData ) {

		// 定义一个返回值
		List<List<String[]>> listStr = new ArrayList<List<String[]>>();

		List<LinkedHashMap<String, Object>> crossLists = pipeCrossDao
				.selectCrossToExcel( bData);
		for(int i=0;i<crossLists.size();i++){
			HashMap<String,Object> result = crossLists.get(i);
			String crossType= CodeUtil.getName((String) result.get("CROSSINGTYPE"));
			String crossTarget=CodeUtil.getName((String) result.get("CROSSINGTARGET"));
			String crossMethod=CodeUtil.getName((String) result.get("CROSSINGMETHOD"));
			String material=CodeUtil.getName((String) result.get("MATERIAL"));
			String pipeStable=CodeUtil.getName((String) result.get("STABLEMEASURES"));
			String coating=CodeUtil.getName((String) result.get("COATINGMATERIAL"));
			result.put("CROSSINGTYPE", crossType);
			result.put("CROSSINGTARGET", crossTarget);
			result.put("CROSSINGMETHOD", crossMethod);
			result.put("MATERIAL", material);
			result.put("STABLEMEASURES", pipeStable);
			result.put("COATINGMATERIAL", coating);
		}

		List<String[]> list = JXL.toList(crossLists, "yyyy-MM-dd HH:mm:ss");

		listStr.add(list);
		return listStr;

	}

	@Override
	public DTOResult getCrossDetails(String crossId) {

		HashMap<String, Object> result = pipeCrossDao
				.selectCrossDetailsById(crossId);
		String crossType=CodeUtil.getName((String) result.get("CROSSINGTYPE"));
		String crossTarget=CodeUtil.getName((String) result.get("CROSSINGTARGET"));
		String crossMethod=CodeUtil.getName((String) result.get("CROSSINGMETHOD"));
		String material=CodeUtil.getName((String) result.get("MATERIAL"));
		String pipeStable=CodeUtil.getName((String) result.get("STABLEMEASURES"));
		String coating=CodeUtil.getName((String) result.get("COATINGMATERIAL"));
		result.put("CROSSINGTYPE", crossType);
		result.put("CROSSINGTARGET", crossTarget);
		result.put("CROSSINGMETHOD", crossMethod);
		result.put("MATERIAL", material);
		result.put("STABLEMEASURES", pipeStable);
		result.put("COATINGMATERIAL", coating);

		return new DTOResult(ExceptionCode.SUCCESS.getCode(),
				ExceptionCode.SUCCESS.getLabel(), result);
	}

	@Override
	public Page<HashMap<String, Object>> getCrossBase(String pipeLineId,
													  String pipeSegmentId, BigDecimal startMileage,
													  BigDecimal endMileage, Integer pageNum, Integer pageSize) {

		int page = 1;

		int size = 10;

		if (pageNum != null) {
			page = pageNum;
		}

		if (pageSize != null) {
			size = pageSize;
		}

		PageHelper.startPage(page, size);

		// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (endMileage!=null && !"".equals(endMileage)) {
			mileageEnd = endMileage;
		}

		return pipeCrossDao.selectBaseByParam(pipeLineId, pipeSegmentId,
				mileageStart, mileageEnd);
	}
}
